package com.yyfax.ph.progress.checkCredit;

import android.text.TextUtils;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * Title: ProgressPresenter Description: Company: YYLending(YouJingPuHui)
 *
 * @author XiongYuFeng
 *         date 2016年5月11日 下午7:08:57
 */
public class CheckCreditPresenter extends Presenter<CheckCreditModel, CheckCreditPresenter.View> {

	protected static final String GET_CHECK_CREDIT_FAILURE = "查看征信失败!";

	public static class Generator implements PresenterFactory<CheckCreditPresenter> {
		@Override
		public CheckCreditPresenter create() {
			return new CheckCreditPresenter();
		}
	}

	public interface View extends IView {

		void onGetCheckCreditSuccess(List<CheckCreditModel.Credit> credits);

		void onGetCheckCreditFailure(String tips);
	}

	private View mView;

	@Override
	public void attach(View view) {
		mView = view;
		mModel = new CheckCreditModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public void getDetailCredit(String rcmdId) {
		CheckCreditModel.Params params = new CheckCreditModel.Params();
		params.rcmdId = rcmdId;
		execute(params, FUNCTIONAL_CODE, true);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void getProductsResult(CheckCreditModel.CreditResult result) {
		if (isResponseSuccess(result)) {
			mView.onGetCheckCreditSuccess(result.getContent());
		} else {
			if (TextUtils.isEmpty(result.getMsg())) {
				mView.onGetCheckCreditFailure(GET_CHECK_CREDIT_FAILURE);
			} else {
				mView.onGetCheckCreditFailure(result.getMsg());
			}
		}
	}
}
